<template>
<div>
  <el-card class="box-card">
  <div slot="header" class="clearfix">
    <span>发布文章</span>
  </div>
  <div class="text item">
    <el-form ref="addFormRef" :model="addFormData" :rules="addFormRules" label-width="120px">
        <el-form-item label="标题" prop="title">
            <el-input v-model="addFormData.title"></el-input>
        </el-form-item>
         <el-form-item label="内容" prop="content">
                 <quillEditor v-model="addFormData.content" ></quillEditor>
        </el-form-item>
         <el-form-item label="封面" >
          <el-radio-group v-model="addFormData.cover.type">
              <el-radio :label="1">单图</el-radio>
              <el-radio :label="3">三图</el-radio>
              <el-radio :label="0">无图</el-radio>
              <el-radio :label="-1">自动</el-radio>
            </el-radio-group>
        </el-form-item>
         <el-form-item label="频道"  prop="channel_id">
          <channel-com @slt="selectHandler"></channel-com>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="addarticle(false)">发布</el-button>
            <el-button @click="addarticle(true)" >存为草稿</el-button>
        </el-form-item>
    </el-form>
  </div>
</el-card>
</div>
</template>

<script>

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import ChannelCom from '@/components/channel.vue'
export default {
  name: 'articleaddList',
  components: {
    quillEditor,
    ChannelCom
  },
  data () {
    return {
      addFormData: {
        title: '',
        content: '',
        cover: {
          type: 0,
          images: []
        },
        channel_id: ''
      },
      addFormRules: {
        title: [
          { required: true, message: '标题必填' },
          // 后端要求title长度介于5-30个字符
          {
            min: 5,
            max: 30,
            message: '标题长度介于5-30个字符'
          }
        ],
        content: [{ required: true, message: '内容必填' }],
        channel_id: [{ required: true, message: '频道必选' }]

      }
    }
  },
  methods: {
    selectHandler (val) {
      console.log(val)
      this.addFormData.channel_id = val
    },
    addarticle (flag) {
      // 表单校验
      this.$refs.addFormRef.validate(valid => {
        if (valid) {
          var pro = this.$http.post('/articles', this.addFormData, {
            params: { draft: flag }
          })
          pro
            .then(result => {
              console.log(result)
              this.$message.success('文章发布成功')
              this.$router.push({ name: 'article' })
            })
            .catch(err => {
              return this.$message.error('发布文章失败' + err)
            })
        }
      })
    }
  }

}
</script>

<style lang="less" scoped>
 .el-form /deep/ .ql-editor{
 height:200px;
}
</style>
